<script setup lang="ts">
import { ref } from "vue";
import { useExample } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Example"
});

const formRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  ExampleDto,
  roleTypes,
  PanelVisibleisshow,
  title,
  onSearch,
  resetForm,
  PanelVisible,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  updateExample
} = useExample();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="bg-bg_color w-[99/100] pl-8 pt-4"
    >
      <el-form-item label="数字" prop="Long">
        <el-input
          v-model="form.Long"
          placeholder="请输入数字"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="文本:" prop="Str1">
        <el-input
          v-model="form.Str1"
          placeholder="请输入文本"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="金额：" prop="Money">
        <el-input
          v-model="form.Money"
          placeholder="请输入金额"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="类型：" prop="RoleType">
        <el-select
          v-model="form.RoleType"
          placeholder="请选择类型"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="item in roleTypes"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      v-model="PanelVisibleisshow"
      :title="title + '样例'"
      width="30%"
      center
    >
      <el-form
        label-position="left"
        label-width="100px"
        :model="ExampleDto"
        style="max-width: 460px"
      >
        <el-form-item label="数字">
          <el-input type="number" v-model="ExampleDto.Long" />
        </el-form-item>
        <el-form-item label="文本">
          <el-input v-model="ExampleDto.Str1" />
        </el-form-item>
        <el-form-item label="金额">
          <el-input type="number" v-model="ExampleDto.Money" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="ExampleDto.Bool"
            active-value="true"
            inactive-value="false"
          />
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            v-model="ExampleDto.RoleType"
            placeholder="请选择类型"
            clearable
          >
            <el-option
              v-for="item in roleTypes"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="ExampleDto.DateTime"
            type="date"
            placeholder="DateTime"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="PanelVisibleisshow = false">取消</el-button>
          <el-button type="primary" @click="updateExample()">
            {{ title }}
          </el-button>
        </span>
      </template>
    </el-dialog>

    <PureTableBar title="样例列表" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="PanelVisible(0)"
        >
          新增
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="columns"
          :checkList="checkList"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="PanelVisible(row)"
            >
              修改
            </el-button>
            <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
